using DotNetty.Buffers;
using static QQ.Codecs.DotNettyByteBufferExtensions;
using static QQ.Utils.MiscHelper;

namespace QQ.Client
{
    public class QQDeviceConfig
    {
        public long appId { get; set; } = 16L;
        public long subAppId { get; set; } = 537062845L;
        public int miscBitMap { get; set; } = 184024956;
        public int mainSigMap { get; set; } = 16724722;
        public int subSigMap { get; set; } = 0x10400;
        public int appClientVersion { get; set; } = 0;
        public string apkID { get; set; } = "com.tencent.mobileqq";
        public string osType { get; set; } = "android";
        public string simInfo { get; set; } = "T-Mobile";
        public string incremental { get; set; } = "5891938";
        public string release { get; set; } = "10";
        public string codeName { get; set; } = "REL";
        public int sdk { get; set; } = 29;
        public string apn { get; set; } = "wifi";
        public string androidId { get; set; } = "Mulan.200122.001"; // display
        public string macAddress { get; set; } = "02:00:00:00:00:00";
        public string wifiBSSID { get; set; } = "02:00:00:00:00:00";
        public string wifiSSID { get; set; } = "<unknown ssid>";
        public string display { get; set; } = "Mulan.200122.001";
        public string product { get; set; } = "Mulan";
        public string device { get; set; } = "Mulan";
        public string board { get; set; } = "Mulan";
        public string brand { get; set; } = "Finch";
        public string model { get; set; } = "Mulan";
        public string bootloader { get; set; } = "unknown";
        public string fingerprint { get; set; } = "Finch/Mulan/Mulan:10/Mulan.200122.001/5891938:user/release-keys";
        public string procVersion { get; set; } = "Linux version 3.0.31-g6fb96c9 (android-build@xxx.xxx.xxx.xxx.com)";
        public byte[] localIP { get; set; } = "7f 00 00 01".HexStringToBytes();//127.0.0.1
        public byte[] ssoIP { get; set; } = "00 01 00 00 00 00 00 7F".HexStringToBytes();
        public NetworkType networkType { get; set; } = NetworkType.WIFI;
        public long openAppID { get; set; } = 715019303L;
        public string apkVersionName { get; set; } = "8.2.0";
        public string buildVer { get; set; } = "8.2.0.1296";
        public byte[] apkSignatureMd5 { get; set; } = "A6 B7 45 BF 24 A2 C2 77 52 77 16 F6 F3 6E B6 8D".HexStringToBytes();
        public string[] domains { get; set; } = {
            "tenpay.com",
            "openmobile.qq.com",
            "docs.qq.com",
            "connect.qq.com",
            "qzone.qq.com",
            "vip.qq.com",
            "qun.qq.com",
            "game.qq.com",
            "qqweb.qq.com",
            "office.qq.com",
            "ti.qq.com",
            "mail.qq.com",
            "qzone.com",
            "mma.qq.com"
        };
        public string ksid { get; set; } = "|454001228437590|A8.2.0.27f6ea96";
        public string baseBand { get; set; } = "";
        public short protocolVersion { get; set; } = 8001;
        public string VendorName { get; set; } = "MIUI";
        public string VendorOSName { get; set; } = "?ONEPLUS A5000_23_17";
        private string bootID;
        private IByteBuffer guid;
        private IByteBuffer tgtgKey;
        private IByteBuffer randomKey;
        private IByteBuffer imsiMd5;
        private string imei;
        public string BootID
        {
            get
            {
                if (bootID == null) bootID = getUUID();
                return bootID;
            }
            private set { }
        }
        public IByteBuffer GUID
        {
            get
            {
                if (guid == null) guid = (androidId + macAddress).ToBytes().MD5().ToByteBuffer();
                return guid.ResetReaderIndex();
            }
            private set { }
        }
        public IByteBuffer RandomKey
        {
            get
            {
                if (randomKey == null) randomKey = getRandomByteArray(16);
                return randomKey.ResetReaderIndex();
            }
            private set { }
        }
        public IByteBuffer ImsiMd5
        {
            get
            {
                if (imsiMd5 == null) imsiMd5 = getRandomByteArray(16).ToArray().MD5().ToByteBuffer();
                return imsiMd5.ResetReaderIndex();
            }
            private set { }
        }
        public string IMEI
        {
            get
            {
                if (imei == null) imei = getRandomNumString(15);
                return imei;
            }
            private set { }
        }
        public IByteBuffer TgtgKey
        {
            get
            {
                if (tgtgKey == null)
                {
                    var tmp = Unpooled.WrappedBuffer(getRandomByteArray(16), GUID);
                    tgtgKey = tmp.ToArray().MD5().ToByteBuffer();
                }
                return tgtgKey.ResetReaderIndex();
            }
            private set { }
        }
        public Qqprotobuf.DeviceInformation deviceInformation
        {
            get => new Qqprotobuf.DeviceInformation
            {
                Bootloader = bootloader.ToProtoString(),
                ProcVersion = procVersion.ToProtoString(),
                CodeName = codeName.ToProtoString(),
                Incremental = incremental.ToProtoString(),
                Fingerprint = fingerprint.ToProtoString(),
                BootID = BootID.ToProtoString(),
                AndroidId = androidId.ToProtoString(),
                BaseBand = baseBand.ToProtoString(),
                InnerVersion = incremental.ToProtoString()
            };
            private set
            {

            }
        }
    }
}